package com.project.service.goodsOrderInfoService.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.AddressBean;
import com.project.bean.GoodsOrderBean;
import com.project.bean.GoodsOrderInfoBean;
import com.project.mapper.addressMapper.IAddressMapper;
import com.project.mapper.goodsOrderInfoMapper.IGoodsOrderInfoMapper;
import com.project.mapper.goodsOrderMapper.IGoodsOrderMapper;
import com.project.service.addressService.IAddressService;
import com.project.service.goodsOrderInfoService.IGoodsOrderInfoService;
import com.project.service.goodsOrderService.IGoodsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class GoodsOrderInfoServicelmpl implements IGoodsOrderInfoService {
    @Autowired
    private IGoodsOrderService orderService;
   @Autowired
   private IGoodsOrderInfoMapper mapper;
   @Autowired
   private IGoodsOrderMapper goodsOrderMapper;

    @Override
    public PageInfo<GoodsOrderInfoBean> findByItem(int pageNO, String state, LocalDate startDate, LocalDate endDate, int userId) {
        PageHelper.startPage(pageNO,PAGESIZE);
        return PageInfo.of(mapper.findByItem(state, startDate, endDate, userId));
    }

    @Override
    public GoodsOrderBean findById(int orderId) {
        GoodsOrderBean goodsOrderBean = goodsOrderMapper.findById(orderId);
        goodsOrderBean.setGoodsOrderInfoList(mapper.findById(orderId));
        return goodsOrderBean;
    }

    @Override
    public void update(int orderInfoId, String state) {
        mapper.update(orderInfoId, state);
        GoodsOrderInfoBean goodsOrderInfo = mapper.findByInfoId(orderInfoId);
        List<GoodsOrderInfoBean> list= mapper.findById(goodsOrderInfo.getGoodsOrderBean().getId());
        for(int i=0;i<list.size();i++){
            if("已完结".equals(list.get(i).getState())){
                if(i == list.size()-1){
                    orderService.update(goodsOrderInfo.getGoodsOrderBean().getId(),"已发货");
                }
                continue;
            }else {
                break;
            }
        }
    }

}
